﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Yipusoft.SecurityEvaluate;

public partial class Police_SecurityEvaluate_CarAnalyse : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 2009; i < 2020; i++)
            {
                dptYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            for (int i = 1; i <= 12; i++)
            {
                dptMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            Bind();
        }

    }
    //周五天数
    private int GetFridayCount(int year, int month)
    {
        int Count = 0;
        DateTime dt = new DateTime(year, month, 1);
        int LastDay = new DateTime(year, month + 1, 1).AddDays(-1).Day;
        for (int i = 1; i <= LastDay; i++)
        {
            if (Convert.ToInt32(new DateTime(year, month, i).DayOfWeek) == 5)
            {
                Count += 1;
                i += 6;
            }
        }
        return Count;

    }
    private double GetCarCountByDate(DateTime Stime, DateTime EndTime, string DepartmentID, double avg)
    {
        double Score = 0;
        ParkLogDataContext db = new ParkLogDataContext();
        List<CarOrder> list = (from obj in db.CarOrder where obj.Date >= Stime && obj.Date <= EndTime && obj.DepartmentID == DepartmentID select obj).ToList();
        if (list == null || list.Count <= 0)
            return 0;
        foreach (CarOrder c in list)
        {
            List<CarInspect> listinspect = (from o in db.CarInspect where o.CID == c.ID select o).ToList();
            double temp = avg / listinspect.Count;
            Score += (temp * listinspect.Where(o => o.Capability == 1).Count());

        }
        return Score;
    }
    private int GetCarCount(string DepartmentID)
    {
        FireBaseDataContext db = new FireBaseDataContext();
        return (from obj in db.Vehicles where obj.DepartmentID.Equals(DepartmentID) select obj).Count();
    }
    private double GetDepartmentScore(int year, int month, string DepartmentID)
    {
        //录入车辆数
        int carCount = GetCarCount(DepartmentID);
        //周天数
        int fridayCount = GetFridayCount(int.Parse(dptYear.SelectedValue), int.Parse(dptMonth.SelectedValue));
        if (carCount == 0)
            return 0;
        double avg = 100 / carCount;
        double Score = fridayCount * 100;
        double sum = 0;

        DateTime dt = new DateTime(year, month, 1);
        int LastDay = new DateTime(year, month + 1, 1).AddDays(-1).Day;
        for (int i = 1; i <= LastDay; i++)
        {
            if (Convert.ToInt32(new DateTime(year, month, i).DayOfWeek) == 5)
            {
                DateTime s = new DateTime(year, month, i).AddDays(-4);
                DateTime e = s.AddDays(6);
                double temp = (GetCarCountByDate(s, e, DepartmentID, avg));
                if (temp != 0)
                    sum += temp;
                i += 6;


            }
        }
        if (Score == 0 || sum == 0)
            return 0;

        return sum;

    }

    private void Bind()
    {
    
        FireMisDataContext db = new FireMisDataContext();
        List<Account_Department> list = (from obj in db.Account_Department where obj.DepartmentID.Substring(4, 1) == "3" select obj).ToList();
        int FCount = GetFridayCount(int.Parse(dptYear.SelectedValue), int.Parse(dptMonth.SelectedValue));
        int AllScore = FCount * 100;
        List<DepartmentScore> listScore = new List<DepartmentScore>();
        foreach (Account_Department a in list)
        {
            double temp = 0;
            temp = GetDepartmentScore(int.Parse(dptYear.SelectedValue), int.Parse(dptMonth.SelectedValue), a.DepartmentID);

            listScore.Add(new DepartmentScore() { DepartmentID = a.DepartmentID, FullName = a.ShortName, Score = temp, AllScore = AllScore });

        }

        var result = (from obj in listScore group obj by obj.DepartmentID.Substring(0, 4) into g select new { g.Key, sumScore = g.Sum(o => o.Score), AllScore = g.Sum(o => o.AllScore), AvgScore = (int)(((int)g.Sum(o => o.AllScore)) == 0 ? 0 : g.Sum(o => o.Score) / g.Sum(o => o.AllScore)), FullName = BLL.Department.GetDepartmentByID(g.Key + "000").ShortName }).ToList();

        Chart1.Series["车辆状况"].Points.DataBindXY(result, "FullName", result, "AvgScore");
        Chart1.Series["车辆状况"].LabelFormat = "p0";

        for (int i = 0; i < result.Count(); i++)
        { 
            Chart1.Series["车辆状况"].Points[i].Url="CarAnalyseLochus.aspx?DepartmentID="+result[i].Key+"&year="+dptYear.SelectedValue+"&month="+dptMonth.SelectedValue;
        }
    }
    protected void lbtnQuery_Click(object sender, EventArgs e)
    {
        Bind();
    }
}

public class DepartmentScore
{
    public string DepartmentID
    {
        set;
        get;
    }
    public string FullName
    {
        set;
        get;
    }
    public double Score
    {
        set;
        get;
    }
    public double AllScore
    {
        set;
        get;
    }
    public double AvgScore
    {
        set;
        get;
    }
}
